package org.example.predicate;

import com.alibaba.fastjson.JSONObject;

import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class Java8Predicate {
    public static void main(String[] args) {
        List<Integer> list = IntStream.range(1,10).boxed().collect(Collectors.toList());
        List<Integer> collect = list.stream().filter(x -> x > 5).collect(Collectors.toList());
        System.err.println(JSONObject.toJSONString(collect));

        Predicate<Integer> func = x -> x > 5;
        List<Integer> collect1 = list.stream().filter(func).collect(Collectors.toList());
        System.err.println(JSONObject.toJSONString(collect1));

        List<Integer> collect2 = list.stream().filter(x -> x > 5 && x < 8).collect(Collectors.toList());
        System.err.println(JSONObject.toJSONString(collect2));

        Predicate<Integer> noGreaterThan5 = x -> x > 5;
        Predicate<Integer> noLessThan8 = x -> x < 8;
        List<Integer> collect3 = list.stream().filter(noGreaterThan5.and(noLessThan8)).collect(Collectors.toList());
        System.err.println(JSONObject.toJSONString(collect3));

        Predicate<String> lengthIs3 = x -> x.length() == 3;
        Predicate<String> startWithA = x -> x.startsWith("A");
        List<String> stringList = Arrays.asList("A", "AA", "AAA", "B", "BB", "BBB");
        List<String> collect4 = stringList.stream().filter(lengthIs3.or(startWithA)).collect(Collectors.toList());
        System.err.println(JSONObject.toJSONString(collect4));

        List<String> collect5 = stringList.stream().filter(startWithA.negate()).collect(Collectors.toList());
        System.err.println(JSONObject.toJSONString(collect5));

        List<String> collect6 = StringProcessor.filter(stringList,x -> x.startsWith("A") && x.length() == 3);
        System.err.println(JSONObject.toJSONString(collect6));


        boolean b = startWithA.or(x -> x.startsWith("m")).test("mkyong");
        System.err.println(b);
        boolean c = startWithA.and(x -> x.length() == 3).negate().test("abc");
        System.err.println(c);
    }
}

class StringProcessor{
    static List<String> filter(List<String> list,Predicate<String> predicate){
        return list.stream().filter(predicate::test).collect(Collectors.toList());
    }
}
